package XOrules;

import java.util.*;
import java.lang.*;
import XOrules.XOrulesUtilits;
import com.sun.java.browser.plugin2.liveconnect.v1.Result;

public class XOgame {
    public Vector<Character> fld;
    public int minChain;
    public Character who;

	public XOgame(int size_, Character whoseFirst_, int minChain_)
    {
		this.fld = XOrulesUtilits.createField(size_);
		this.who = whoseFirst_;
		if (minChain_ == 0)
			this.minChain = size_;
		else
			this.minChain = minChain_;
    }

	public XOgame(int size_, Character whoseFirst_)
    {
		this(size_, whoseFirst_, 0);
    }

	public boolean makeTurn(Character who_, int x, int y)
    {
		if (this.who != who)
			return false;
		if (!XOrulesUtilits.isItTurn(fld, who, x, y))
			return false;
		if (isItWin())
			return false;
		XOrulesUtilits.makeTurn(fld, who, x, y);
		flipTurn();
		return true;
    }

	public void flipTurn()
    {
		if (who == XOrulesUtilits.Xchar)
			who = XOrulesUtilits.Ochar;
		else
			this.who = XOrulesUtilits.Xchar;
    }

	public boolean isItWin()
    {
		Character whoseWin_ = XOrulesUtilits.whoseWin(fld, minChain);
		return !(whoseWin_ == XOrulesUtilits.Fchar) && !(whoseWin_ == XOrulesUtilits.NBchar);
    }

	public Character whoseWin()
    {
		return XOrulesUtilits.whoseWin(fld, minChain);
    }

	public String getSituation()
    {
	    StringBuilder res = new StringBuilder("");
		for (Character c : fld)
			res.append(c);
		res.append(":");
        res.append(who);
		return res.toString();
    }
}

